﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PictureOrganizer2.DataContainer;
using System.Configuration;
using System.Drawing;

namespace PictureOrganizer2.Forms
{
    public partial class ConfigFavFoldersForm : Form
    {


        public ConfigFavFoldersForm()
        {
            InitializeComponent();

            this.Icon = Icon.FromHandle(Images.folder_wrench.GetHicon());
            this.favoriteFoldersTableAdapter.Fill(dbSet.FavoriteFolders);
            this.dataListView.Sort(this.olvColumnSortOrder);
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            int nextId = this.dbSet.FavoriteFolders.Count + 1;
            this.dbSet.FavoriteFolders.AddFavoriteFoldersRow(nextId, "Favorite", nextId, "");
        }

        private void btn_Remove_Click(object sender, EventArgs e)
        {
            if (this.dataListView.SelectedObject != null)
            {
                this.dbSet.FavoriteFolders.RemoveFavoriteFoldersRow(this.dbSet.FavoriteFolders[this.dataListView.SelectedIndex]);
                this.dataListView.SelectedIndex = this.dataListView.Items.Count - 1;
            }
        }

        private void btn_Up_Click(object sender, EventArgs e)
        {
            if (this.dataListView.SelectedObject  != null)
            {
                if (this.dataListView.SelectedIndex == 0) return;

                SwitchItems(true);
            }
        }

        private void btn_Down_Click(object sender, EventArgs e)
        {
            if (this.dataListView.SelectedObject != null)
            {
                if (this.dbSet.FavoriteFolders.Count <= this.dataListView.SelectedIndex + 1) return;

                SwitchItems(false);
            }
        }

        private void SwitchItems(Boolean switchUp)
        {
            int i = 1;
            if (switchUp) i = -1;
            //this.listBox.s
            int index = this.dataListView.SelectedIndex + 1;

            var row1 = (from f in this.dbSet.FavoriteFolders where f.SortOrder == index select f).First();
            var row2 = (from f in this.dbSet.FavoriteFolders where f.SortOrder == (index + i) select f).First();
            row1.SortOrder = (index + i);
            row2.SortOrder = (index);

            this.dataListView.Sort(this.olvColumnSortOrder);

            //this.dbSet.FavoriteFolders[index].SortOrder = (index + 1) + i;
            //this.dbSet.FavoriteFolders[index + i].SortOrder = (index + 1);
            //this.dataListView.SelectedIndex = index + i;
        }

        private void btn_SelectDirectory_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(this.tb_Directory.Text))
            {
                folderBrowserDialog.SelectedPath = this.tb_Directory.Text;
            }

            if (folderBrowserDialog.ShowDialog(this) == DialogResult.OK)
            {
                this.tb_Directory.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void btn_Ok_Click(object sender, EventArgs e)
        {
            this.favoriteFoldersTableAdapter.Update(dbSet.FavoriteFolders);
        }

        private void tb_Name_TextChanged(object sender, EventArgs e)
        {
            //this.dataListView.re
            //this.dataListView.Update();
        }
    }
}
